package com.kipling.kio.core.intf;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

import com.kipling.kio.core.GroupContext;
import com.kipling.kio.core.exception.KioDecodeException;
import com.kipling.kio.server.ServerGroupContext;

/**
 * The Interface KioHandler.
 *
 * @param <Ext> the generic type
 * @param <P> the generic type
 * @param <R> the generic type
 */
public interface KioHandler<P extends Packet, R>
{

	/**
	 * 处理消息包
	 *
	 * @param packet the packet
	 * @return the r
	 * @author: tanyaowu
	 */
	R handler(Packet packet,ServerGroupContext< P, R> serverGroupContext) throws Exception;
	
	/**
	 * 处理客户端请求
	 * @param selectionKey
	 * @throws Exception
	 */
	void handlerSelectionKey(SocketChannel socketChannel,ByteBuffer byteBuffer,ServerGroupContext< P, R> serverGroupContext) throws Exception;
	
	

	/**
	 * 编码
	 *
	 * @param packet the packet
	 * @return the byte buffer
	 * @author: tanyaowu
	 */
	ByteBuffer encode(P packet, GroupContext< P, R> groupContext);

	/**
	 * 根据ByteBuffer解码成业务需要的Packet对象.
	 *
	 * @param buffer the buffer
	 * @return the t
	 * @throws KioDecodeException the aio decode exception
	 */
	P decode(ByteBuffer buffer) throws KioDecodeException;

}
